package com.example.javaproject.datastructure.array;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Description:
 * <br/>Author:  tianjinyong
 * <br/>Date:    2020/6/30 11:02 PM
 */
public class CopyOnWriteArrayListDemo {

    public static void main(String[] args) {
        testModifyWhentraverse();
    }

    /**
     *  演示CopyOnWriteArrayList迭代期间可以修改集合的内容
     */
    public static void testModifyWhentraverse() {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(new Integer[]{1, 2, 3, 4});
        System.out.println(list);// [1,2,3,4]

        Iterator<Integer> iteratorOne = list.iterator();
        list.add(5);

        System.out.println(list);//[1,2,3,4,5]
        Iterator<Integer> iteratorTwo = list.iterator();
        System.out.println("iteratorOne--");
        iteratorOne.forEachRemaining(x -> {
            System.out.println(x);//[1,2,3,4]
        });

        System.out.println("iteratorTwo--");
        iteratorTwo.forEachRemaining(x -> {
            System.out.println(x); //[1,2,3,4,5]
        });
    }


}
